package oldreturn

import (
	"fmt"

	"oldReturn/src/tools/DB"
)

type UserInfo struct {
	UserId    string
	PartnerId int
	PlayerId  string
	LoginTime string
	IsJoin    int
}

var DBA = DB.GetDBManager()

func QueryUser(UserId string, PartnerId int) (UserInfo, error) {
	var user = UserInfo{}
	sql := fmt.Sprintf("select * from userInfo where UserId = %s AND PartnerId=%d ORDER BY LoginTime DESC LIMIT 1", UserId, PartnerId)
	rows, err := DBA.Query(sql)

	if err != nil {
		fmt.Println(err)
	}
	for rows.Next() {
		err := rows.Scan(&user.UserId, &user.PartnerId, &user.PlayerId, &user.LoginTime, &user.IsJoin)
		if err != nil {
			fmt.Println(err)
		}
	}
	rows.Close()
	return user, err
}

func SetUserJoinAct(UserId string, PartnerId int) error {

	sql := fmt.Sprintf("UPDATE userinfo SET isJoin = %d WHERE UserId = %s AND PartnerId=%d", 1, UserId, PartnerId)
	_, err := DBA.Update(sql)
	if err != nil {
		fmt.Println(err)
	}
	return err
}
